var PeopleMedia = PeopleMedia || {};
PeopleMedia.Slideshow = new function () {
    var CurrentDisplayMemberId;
    var CurrentDisplayMediaId;
    var CurrenSiteId;
    var Offset;
    this.InitialMemberAction = "";
    this.BackAction = "";
    this.NextAction = "";
    this.SaveAction = "";
    this.init = function () {
        $jq("#nofavorite").click(this.save);
        $jq("#slideshowrighthide").click(this.next);
        $jq("#slideshowlefthide").click(this.previous);
        $jq("#PhotoLikeBtn").one("click", this.like);

    };
    this.getInitialMember = function () {
        PeopleMedia.Slideshow.getData(PeopleMedia.Slideshow.InitialMemberAction);
    };
    this.inProgress = false;
    this.like = function () {

        $jq.ajax({
            type: 'POST',
            url: PeopleMedia.Slideshow.LikeAction,
            dataType: 'json',
            data: {
                mediaId: CurrentDisplayMediaId,
                encodedMemberId: CurrentDisplayMemberId,
                siteId: CurrenSiteId
            },
            success: function (result) {
                if (result) {    
                    if (result.redirect) {
                        location.href = result.redirect;
                    }
                    if (result.Success == true || result.Status == 'Success') {
                        //$jq(".SlideshowSaveButton").addClass("favoriteclicked").attr('title', 'Saved');
                        $jq('#PhotoLikeBtn').hide();
                        $jq('#PhotoLikeBtnclicked').show();
                        $jq('#PhotoLikeBtnclicked').removeAttr('style');
                        PeopleMedia.Slideshow.inProgress = false;
                        PeopleMedia.Slideshow.next();
                        $jq("#PhotoLikeBtn").one("click", PeopleMedia.Slideshow.like);
                    }
                    else if (result.Message) {
                        $jq("#PhotoLikeBtn").one("click", PeopleMedia.Slideshow.like);
                        alert(result.Message);
                    }
                }
            }
        });
    };
    this.save = function () {
        if (PeopleMedia.Slideshow.inProgress) return;
        PeopleMedia.Slideshow.inProgress = true;
        $jq.ajax({
            type: 'POST',
            url: PeopleMedia.Slideshow.SaveAction,
            success: function (result) {
                if (result) {
                    PeopleMedia.Slideshow.inProgress = false;
                    if (result.redirect) {
                        location.href = result.redirect;
                    }
                    if (result.Status == 'Success') {
                        //$jq(".SlideshowSaveButton").addClass("favoriteclicked").attr('title', 'Saved');
                        $jq('#nofavorite').hide();
                        $jq('#favoriteclicked').show();
                        $jq('#favoriteclicked').removeAttr('style');

                        PeopleMedia.Slideshow.next();
                    }
                    else if (result.Message) {
                        alert(result.Message);
                    }
                }
            }
        });
    };
    this.next = function () {
        if (PeopleMedia.Slideshow.inProgress) return;
        PeopleMedia.Slideshow.inProgress = true;
        PeopleMedia.Slideshow.getData(PeopleMedia.Slideshow.NextAction);
    };
    this.previous = function () {
        if (PeopleMedia.Slideshow.inProgress) return;
        PeopleMedia.Slideshow.inProgress = true;
        PeopleMedia.Slideshow.getData(PeopleMedia.Slideshow.BackAction);
    };

    this.getData = function (actionUrl) {
        $jq.ajax({
            type: 'POST',
            url: actionUrl,
            dataType: 'json',
            data: {offset:Offset},
            success: function (result) {
                PeopleMedia.Slideshow.SetData(result);
            }
        });
    };


    this.SetData = function (data) {
        var prevMember;
        var nextMember;
        var currentMember;
        if (data) {
            if (data.redirect) {
                location.href = data.redirect;
            }
            if (data.Status == "Success" && data.CurrentMember) {

                $jq(".slideshowCurrentMemberProfileLink").attr("href", data.CurrentMember.ProfileUrl);
                $jq(".ImInterestedButton").attr("href", data.CurrentMember.ImInterestedUrl);
                //$jq("#LikeObjectId").val(data.CurrentMember.MediaId);
                CurrentDisplayMemberId = data.CurrentMember.EncodedMemberId;
                CurrentDisplayMediaId = data.CurrentMember.MediaId;
                CurrenSiteId = data.CurrentMember.SiteId;
                Offset = data.Offset;
                var stage = $jq("#iminterestedslideshowpeople");
                stage.unbind();
                stage.click(function () {
                    location.href = data.CurrentMember.ProfileUrl;
                });
                if (data.PreviousMember && data.NextMember) {
                    stage.html('');

                    prevMember = "<img src='" + data.PreviousMember.PhotoUrl + "' />";
                    stage.append(prevMember);
                    currentMember = "<img src='" + data.CurrentMember.PhotoUrl + "' />";
                    stage.append(currentMember);

                    nextMember = "<img src='" + data.NextMember.PhotoUrl + "' />";
                    stage.append(nextMember);
                    PeopleMedia.Slideshow.inProgress = false;
                }
                else if (data.PreviousMember) {
                    prevMember = "<img style='display:none;' src='" + data.PreviousMember.PhotoUrl + "' />";
                    stage.html(prevMember + stage.html());
                    stage.find(">:first-child").animate({ width: 'show' }, function () {
                        stage.find(">:last-child").remove();
                        PeopleMedia.Slideshow.inProgress = false;
                    });

                }
                else if (data.NextMember) {
                    nextMember = "<img src='" + data.NextMember.PhotoUrl + "' />";
                    stage.append(nextMember);
                    stage.find(">:first-child").animate({ width: 'hide' }, function () {
                        stage.find(">:first-child").remove();
                        PeopleMedia.Slideshow.inProgress = false;
                    });
                }
                $jq("#slideshowCurrentMemberImage").attr("src", data.CurrentMember.PhotoUrl);
                if (data.CurrentMember.IsFavorite) {
                    // $jq(".SlideshowSaveButton").addClass("favoriteclicked").attr('title', 'Saved');
                    $jq('#nofavorite').hide();
                    $jq('#favoriteclicked').show();
                    $jq('#favoriteclicked').removeAttr('style');
                } else {
                    // $jq(".SlideshowSaveButton").removeClass("favoriteclicked").attr('title', 'Save');
                    $jq('#nofavorite').show();
                    $jq('#favoriteclicked').hide();
                    $jq('#favoriteclicked').attr('style', 'display:none');
                }

                if (data.CurrentMember.IsLiked) {
                    $jq('#PhotoLikeBtn').hide();
                    $jq('#PhotoLikeBtnclicked').show();
                    $jq('#PhotoLikeBtnclicked').removeAttr('style');
                    $jq('#PhotoLikeBtnclicked').unbind();

                } else {

                    $jq('#PhotoLikeBtn').show();
                    $jq('#PhotoLikeBtnclicked').hide();
                    $jq('#PhotoLikeBtnclicked').attr('style', 'display:none');

                }


            } else {
                $jq("#iminterestedarea").hide('fast');
            }
        } else {
            $jq("#iminterestedarea").hide('fast');
        }
    };
};
                              
